package cn.supermarket.controller.service;

import cn.supermarket.common.annotation.Log;
import cn.supermarket.common.config.ProjectConfig;
import cn.supermarket.common.core.controller.BaseController;
import cn.supermarket.common.core.entity.Result;
import cn.supermarket.common.core.page.TableDataInfo;
import cn.supermarket.common.enums.BusinessType;
import cn.supermarket.common.utils.SecurityUtils;
import cn.supermarket.common.utils.file.FileUploadUtils;
import cn.supermarket.common.utils.file.MimeTypeUtils;
import cn.supermarket.system.dto.ProductImgDTO;
import cn.supermarket.system.dto.TProductDTO;
import cn.supermarket.system.entity.TProduct;
import cn.supermarket.system.service.TProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @Author yp
 * @Date 2025/2/27:08:57
 */
@RestController
@RequestMapping("/service/product")
@Api(value = "商品模块", tags = "商品模块")
public class TProductController extends BaseController {
    @Autowired
    private TProductService tProductService;

    @GetMapping(value = "/getSelection")
    public Result getSelection()
    {
        return success(tProductService.getSelection());
    }

    @GetMapping("/list")
    public TableDataInfo list(TProductDTO tProductDTO)
    {
        List<TProduct> list = tProductService.pageList(tProductDTO);
        return getDataTable(list);
    }

    /**
     * 根据岗位编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('service:product:query')")
    @GetMapping(value = "/{productInformationId}")
    public Result getInfo(@PathVariable String productInformationId)
    {
        return success(tProductService.getById(productInformationId));
    }

    /**
     * 新增
     */
    @PreAuthorize("@ss.hasPermi('service:product:add')")
    @Log(title = "商品管理", businessType = BusinessType.INSERT)
    @PostMapping
    public Result add(@Validated @RequestBody TProduct tProduct)
    {
        tProduct.setCreateBy(SecurityUtils.getUsername());
        return toAjax(tProductService.save(tProduct));
    }

    /**
     * 修改
     */
    @PreAuthorize("@ss.hasPermi('service:product:edit')")
    @Log(title = "商品管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public Result edit(@Validated @RequestBody TProduct tProduct)
    {
        tProduct.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(tProductService.updateById(tProduct));
    }

    /**
     * 删除
     */
    @PreAuthorize("@ss.hasPermi('service:product:remove')")
    @Log(title = "商品管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{products}")
    public Result remove(@PathVariable String[] products)
    {
        return toAjax(tProductService.removeByIds(Arrays.asList(products)));
    }

    /**
     * 上传商品图片
     */
    @PreAuthorize("@ss.hasPermi('service:product:update')")
    @Log(title = "商品管理", businessType = BusinessType.UPDATE)
    @PostMapping("/img")
    @ApiOperation(value = "上传商品图片", notes = "上传商品图片")
    public Result img(ProductImgDTO productImgDTO) throws Exception{
        TProduct tProduct = new TProduct();
        tProduct.setProductInformationId(productImgDTO.getProductInformationId());
        tProduct.setCartImg((Objects.requireNonNull(productImgDTO.getImg().getOriginalFilename()).length()!=0)?FileUploadUtils.upload(ProjectConfig.getProfile()+"/"+productImgDTO.getProductInformationId()+"/",productImgDTO.getImg(), MimeTypeUtils.IMAGE_EXTENSION):null);
        tProduct.setCartImg1((Objects.requireNonNull(productImgDTO.getImg1().getOriginalFilename()).length()!=0)?FileUploadUtils.upload(ProjectConfig.getProfile()+"/"+productImgDTO.getProductInformationId()+"/",productImgDTO.getImg1(), MimeTypeUtils.IMAGE_EXTENSION):null);
        tProduct.setCartImg2((Objects.requireNonNull(productImgDTO.getImg2().getOriginalFilename()).length()!=0)?FileUploadUtils.upload(ProjectConfig.getProfile()+"/"+productImgDTO.getProductInformationId()+"/",productImgDTO.getImg2(), MimeTypeUtils.IMAGE_EXTENSION):null);
        tProduct.setCartImg3((Objects.requireNonNull(productImgDTO.getImg3().getOriginalFilename()).length()!=0)?FileUploadUtils.upload(ProjectConfig.getProfile()+"/"+productImgDTO.getProductInformationId()+"/",productImgDTO.getImg3(), MimeTypeUtils.IMAGE_EXTENSION):null);
        tProduct.setCartImg4((Objects.requireNonNull(productImgDTO.getImg4().getOriginalFilename()).length()!=0)?FileUploadUtils.upload(ProjectConfig.getProfile()+"/"+productImgDTO.getProductInformationId()+"/",productImgDTO.getImg4(), MimeTypeUtils.IMAGE_EXTENSION):null);
        tProduct.setCartImg5((Objects.requireNonNull(productImgDTO.getImg5().getOriginalFilename()).length()!=0)?FileUploadUtils.upload(ProjectConfig.getProfile()+"/"+productImgDTO.getProductInformationId()+"/",productImgDTO.getImg5(), MimeTypeUtils.IMAGE_EXTENSION):null);
        tProduct.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(tProductService.updateById(tProduct));
    }
}
